<%--
  Created by IntelliJ IDEA.
  User: shilongliu
  Date: 2021/10/15
  Time: 16:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="./js/wangEditor.min.js"></script>
    <script src="./js/jquery-3.6.0.min.js"></script>
    <style>
        #p1{
            font-size: 8px;
            color: #999999;
            margin: 5px 50px;
        }
        #div1{
            margin: 0px 50px 50px 50px;
        }

    </style>
</head>

<body>

<textarea id="text1" style="width:100%; height:200px;"></textarea>
<p id="p1">上传大小限制:图片大小<10MB,视频大小<1000MB</p>
<div id="div1">
    ${msg}
</div>
<input type="button" id="tijiao" value="修改"/>

<script type="text/javascript">
    const E = window.wangEditor;
    const editor = new E('#div1');
    var $text1 = $('#text1');
    editor.config.uploadImgServer = 'addImg.msg';
    editor.config.uploadVideoServer = 'addVideo.msg';
    editor.config.uploadImgHooks = {
        fail:function (xhr, editor, result) {
            alert("上传图片失败");
        }
    };
    editor.config.uploadVideoHooks = {
        fail:function (xhr, editor, result) {
            alert("上传视频失败");
        }
    }

    editor.config.customUploadImg = function(files, insert) {
        var formData = new FormData();
        for (var i = 0; i < files.length; i++) {
            formData.append("files[" + i + "]", files[i], files[i].name);
        }

        $.ajax({
            url: 'addImg.msg',    //这里是个servlet
            type: "POST",
            data: formData,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success: function (da) {
                console.log(da);
                if (da.errno == 0) {
                    for (var j = 0; j < da.data.length; j++) {
                        insert(da.data[j]);
                    }
                } else {
                    //alert(da.msg);
                    return;
                }
            }
        });
    }
    editor.config.uploadImgMaxSize = 10 * 1024 * 1024;
    editor.config.uploadImgMaxLength = 10;

    editor.config.customUploadVideo = function (files, insert){
        var formData = new FormData();
        for (var i = 0; i < files.length; i++) {
            formData.append("files[" + i + "]", files[i], files[i].name);
        }

        $.ajax({
            url: 'addVideo.msg',    //这里是个servlet
            type: "POST",
            data: formData,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success: function (da) {
                console.log(da);
                if (da.errno == 0) {
                    for (var j = 0; j < da.data.length; j++) {
                        insert(da.data[j]);
                    }
                } else {
                    //alert(da.msg);
                    return;
                }
            }
        });
    }

    editor.config.onchange = function (html) {
        // 监控变化，同步更新到 textarea
        $text1.val(html);
    }

    editor.create();

    $text1.val(editor.txt.html());
    $("#tijiao").click(function(){

        var content= $('#text1').val();
        console.log(content);
        //alert(content);
        $.ajax({
            type:"post",
            url:"updateMessage.msg",
            data:{
                "msg":content,
                "id":${id}
            },
            success: function(result){
                location.assign("index.html");
            },
        });
    })
</script>
</body>
</html>
